﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Saxxon.LibDoomSharp
{
    public class WadDirectory : SerializableBase, IList<WadDirectoryEntry>
    {
        /// <summary>
        /// Instantiate a WadDirectory.
        /// </summary>
        internal WadDirectory(Wad wad)
        {
            this.Wad = wad;
        }

        /// <summary>
        /// List of Wad Directory Entries.
        /// </summary>
        protected List<WadDirectoryEntry> Entries { get; set; }

        /// <summary>
        /// Read Wad Directory.
        /// </summary>
        protected override void Read(Stream stream, int length)
        {
            WadDirectoryEntry[] entries = new WadDirectoryEntry[length / WadDirectoryEntry.ENTRY_LENGTH];
            int count = entries.Length;
            for (int i = 0; i < count; i++)
            {
                entries[i] = new WadDirectoryEntry();
                entries[i].ReadFrom(stream, WadDirectoryEntry.ENTRY_LENGTH);
            }

            Entries = new List<WadDirectoryEntry>(entries);
        }

        /// <summary>
        /// Write Wad Directory.
        /// </summary>
        protected override void Write(Stream stream)
        {
            if (Entries != null)
            {
                foreach (WadDirectoryEntry entry in Entries)
                {
                    if (entry != null)
                    {
                        entry.WriteTo(stream);
                    }
                }
            }
        }

        /// <summary>
        /// Wad that the WadDirectory belongs to.
        /// </summary>
        public Wad Wad { get; protected set; }

        #region IList Inherited Members
        public int IndexOf(WadDirectoryEntry item)
        {
            return Entries.IndexOf(item);
        }

        public void Insert(int index, WadDirectoryEntry item)
        {
            Entries.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            Entries.RemoveAt(index);
        }

        public WadDirectoryEntry this[int index]
        {
            get
            {
                return Entries[index];
            }
            set
            {
                Entries[index] = value;
            }
        }

        public void Add(WadDirectoryEntry item)
        {
            Entries.Add(item);
        }

        public void Clear()
        {
            Entries.Clear();
        }

        public bool Contains(WadDirectoryEntry item)
        {
            return Entries.Contains(item);
        }

        public void CopyTo(WadDirectoryEntry[] array, int arrayIndex)
        {
            Entries.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return Entries.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(WadDirectoryEntry item)
        {
            return Entries.Remove(item);
        }

        public IEnumerator<WadDirectoryEntry> GetEnumerator()
        {
            return Entries.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return Entries.GetEnumerator();
        }
        #endregion
    }
}
